package com.nidu.demo.notify.executor.command;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.notify.ability.NotifyTemplateDomainService;
import com.nidu.demo.notify.convertor.NotifyTemplateConvertor;
import com.nidu.demo.notify.dto.NotifyTemplateCO;
import com.nidu.demo.notify.dto.NotifyTemplateUpdateCmd;
import com.nidu.demo.notify.gateway.NotifyTemplateGateway;
import com.nidu.demo.notify.model.NotifyTemplate;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 站内信更新命令执行器
 *
 * @author auto
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class NotifyTemplateUpdateCmdExe {

    private final NotifyTemplateConvertor convertor;

    private final NotifyTemplateGateway gateway;

    private final NotifyTemplateDomainService domainService;

    public Response execute(NotifyTemplateUpdateCmd cmd) {
        NotifyTemplate template = NotifyTemplate.update(cmd.getId(), cmd.getTemplateName(), cmd.getTemplateCode(), cmd.getTemplateNickname(),
                cmd.getTemplateContent(), cmd.getTemplateType(), cmd.getRemark());

        domainService.validateDuplicate(template.getTemplateCode(), template.getId());

        NotifyTemplate notifyTemplate = gateway.update(template);

        NotifyTemplateCO notifyTemplateCO = convertor.toClientObject(notifyTemplate);

        return SingleResponse.of(notifyTemplateCO);
    }
}
